//
//  YEMIndexSlideView.m
//  YueEnjoysMusicApp
//
//  Created by brother on 16/2/29.
//  Copyright © 2016年 brother. All rights reserved.
//

#import "YEMIndexSlideView.h"
#import "SlideBarCell.h"
#import "YEMRecommendTableView.h"


#define kMyViewFrameW self.myViewFrame.size.width
#define kMyViewFrameH self.myViewFrame.size.height*0.918
#define kMyViewFrameY self.myViewFrame.origin.y
#define kMyTopScrollViewH 40
#define kTopBtnCount 4
#define kUnderViewH 60

@interface YEMIndexSlideView()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

// 上方显示按钮的title数组
@property (nonatomic,strong) NSArray *btnTitleArray;

// 整个视图的大小
@property (nonatomic,assign) CGRect myViewFrame;

// 上方的按钮数组 添加上面的按钮 里面放的是‘View'
@property (strong, nonatomic) NSMutableArray *topButtonArray;

// 上方的view
@property (strong, nonatomic) UIView *topView;

// 上方的ScrollView
@property (nonatomic,strong) UIScrollView *myTopScrollView;

// 下方的ScrollView
@property (nonatomic,strong) UIScrollView *myDownScrollView;

// 下方的按钮数组 添加上面的按钮 里面放的是‘TableView'
@property (strong, nonatomic) NSMutableArray *downTableViewArray;

// 上方的ScrollView下面滑动的View
@property (strong, nonatomic) UIView *slideView;

// 下方的myDownScrollView当前选中页数
@property (assign,nonatomic) NSInteger currentPage;

// 最下方存放播放器页面的View
@property (strong, nonatomic) UIView *underView;

@property (strong, nonatomic) SlideBarCell *cell11;

@property (strong, nonatomic) NSArray *oneGroupArray;


@end

@implementation YEMIndexSlideView
/*
 **********************++++vvv懒加载vvv++++******************************
 */

- (NSArray *)oneGroupArray {

    if (_oneGroupArray == nil) {
        _oneGroupArray = @[@"SlideBarCell",@"HYTwoGroupTableViewCell",@"HYOneGroupCell",@"HYOneGroupCell",@"HYOneGroupCell",@"HYOneGroupCell"];
    }
    return _oneGroupArray;
}

- (NSArray *)btnTitleArray {
    
    if (_btnTitleArray == nil) {
        NSArray *myArray = @[@"推荐",@"歌手",@"榜单",@"电台"];
        _btnTitleArray = myArray;
    }
    return _btnTitleArray;
}

- (NSArray *)topButtonArray {

    if (_topButtonArray == nil) {
        _topButtonArray = [NSMutableArray array];
    }
    return _topButtonArray;
}

- (NSMutableArray *)downTableViewArray {

    if (_downTableViewArray == nil) {
        _downTableViewArray = [NSMutableArray array];
    }
    return _downTableViewArray;
}

/*
 ***********************++++^^^懒加载^^^++++******************************
*/

- (void)loadTheLazyLoading {

    [self topView];
    [self btnTitleArray];
    [self topButtonArray];
    [self downTableViewArray];
    [self slideView];
}

- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        
        _myViewFrame = frame;
        NSLog(@"%f",frame.size.width);
        NSLog(@"%f",frame.size.height);
        [self initUnderView];
        [self loadTheLazyLoading];
        [self initDownScrollView];
        [self initTopTabs];
        [self initDownTableView];
        [self initSlideView];
        
    }
    return self;
}

#pragma mark -- 实例化下面的myDownScrollView和underView
-(void) initDownScrollView {

    UIScrollView *tempScrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kMyViewFrameY-20, kMyViewFrameW , kMyViewFrameH - kMyTopScrollViewH - kUnderViewH)];
    // 设置边界没有滑动的效果
    tempScrollerView.bounces = NO;
    self.myDownScrollView = tempScrollerView;
    // 以下减的‘20’  是状栏的高度
    self.myDownScrollView.contentSize = CGSizeMake(kMyViewFrameW * kTopBtnCount, kMyViewFrameH - kMyTopScrollViewH - kUnderViewH - 20);
    
    // 启用分页
    self.myDownScrollView.pagingEnabled = YES;
    self.myDownScrollView.delegate = self;
    [self addSubview:self.myDownScrollView];
}

#pragma mark -- 实例化下面的underView
- (void)initUnderView {

    UIView *tempUnder = [[UIView alloc]initWithFrame:CGRectMake(0, kMyViewFrameH-kUnderViewH, kMyViewFrameW, kUnderViewH)];
    tempUnder.backgroundColor = [UIColor purpleColor];
    self.underView = tempUnder;
    [self addSubview:tempUnder];
    
}

#pragma mark -- 实例化顶部的myTopScrollView和button
-(void) initTopTabs {

    CGFloat topWidth = kMyViewFrameW / kTopBtnCount;
    
    UIView *tempViewUp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kMyViewFrameW, kMyTopScrollViewH)];
    self.topView = tempViewUp;
    
    UIScrollView *tempScrollerViewUp = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kMyViewFrameW, kMyTopScrollViewH)];
    self.myTopScrollView = tempScrollerViewUp;
    // 显示垂直、水平滚动指标
    self.myTopScrollView.showsHorizontalScrollIndicator = NO;
    self.myTopScrollView.showsVerticalScrollIndicator = YES;
    self.myTopScrollView.bounces = NO;
    self.myTopScrollView.delegate = self;
    
    self.myTopScrollView.contentSize = CGSizeMake(kMyViewFrameW, kMyTopScrollViewH);
    
    [self addSubview:self.topView];
    [self.topView addSubview:self.myTopScrollView];
    
    for (int i = 0; i < kTopBtnCount; i++) {
        UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(i * topWidth,0, topWidth, kMyTopScrollViewH)];
        tempView.backgroundColor = [UIColor lightGrayColor];
        if (i % 2) {
            tempView.backgroundColor = [UIColor grayColor];
        }

        UIButton *topBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, topWidth, kMyTopScrollViewH)];
        topBtn.tag = i;
        [topBtn setTitle:[NSString stringWithFormat:@"%@",self.btnTitleArray[i]] forState:UIControlStateNormal];
        
        [topBtn addTarget:self action:@selector(topBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [tempView addSubview:topBtn];
        
        [self.topButtonArray addObject:tempView];
        [self.myTopScrollView addSubview:tempView];
    }
}

#pragma mark --点击顶部的按钮所触发的方法
- (void)topBtnClick:(id)sender {

    UIButton *button = sender;
    [self.myDownScrollView setContentOffset:CGPointMake(button.tag * kMyViewFrameW, 0) animated:YES];
}

#pragma mark --初始化下方myDownScrollView中的TableView
- (void)initDownTableView {

    // 初始化   TableView就是用来重用的
    for (int i = 0; i < 4; i++) {
        UITableView *tempTableView = [[UITableView alloc]initWithFrame:CGRectMake(i * kMyViewFrameW, 0, kMyViewFrameW, kMyViewFrameH - kMyTopScrollViewH - kUnderViewH)];
//        tempTableView.backgroundColor = [UIColor colorWithRed:10*i/255 green:208i/255 blue:308i/255 alpha:1.0];
        tempTableView.delegate = self;
        tempTableView.dataSource = self;
        tempTableView.tag = i;
        
        [self.downTableViewArray addObject:tempTableView];
        [self.myDownScrollView addSubview:tempTableView];
    }
}

#pragma mark --根据scrollView的滚动位置重用tableView，减少内存开支
- (void)updateTableWithPageNumber: (NSUInteger)pageNumber {

    int tabviewTag = pageNumber % 4;
    CGRect tableViewNewFrame = CGRectMake(pageNumber * kMyViewFrameW, 0, kMyViewFrameW, kMyViewFrameH - kMyTopScrollViewH);
    
    UITableView *tempTableView = self.downTableViewArray[tabviewTag];
    tempTableView.frame = tableViewNewFrame;
    [tempTableView reloadData];
}

#pragma mark -- 初始化滑动的指示View
- (void)initSlideView {

    CGFloat width = kMyViewFrameW / kTopBtnCount;
    UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(0, kMyTopScrollViewH - 5, width , 5)];
    self.slideView = tempView;
    self.slideView.backgroundColor = [UIColor greenColor];
    [self.myTopScrollView addSubview:self.slideView];
}

#pragma mark -- scrollView的代理方法
- (void)modifyTopScrollViewPositiong: (UIScrollView *)scrollView {
    
    if ([self.myTopScrollView isEqual:scrollView]) {
        CGFloat contentOffSetX = self.myTopScrollView.contentOffset.x;
        CGFloat width = self.slideView.frame.size.width;
        
        int tempcount = (int)contentOffSetX / (int)width;
        
        CGFloat step = (int)contentOffSetX % (int)width;
        CGFloat sumStep = tempcount * width;
        if (step > width/2 ) {
            sumStep = width * (tempcount + 1);
        }
        [self.myTopScrollView setContentOffset:CGPointMake(sumStep, 0) animated:YES];
        return;
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    NSLog(@"scrollViewDidEndDragging---x:::%f   Y:::%f",self.myDownScrollView.frame.size.width,self.myDownScrollView.frame.size.height);
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

    [self scrollViewDidEndDecelerating:scrollView];
    NSLog(@"scrollViewDidEndScrollingAnimation---x++++%f   Y++++%f",self.myDownScrollView.frame.origin.x,self.myDownScrollView.frame.origin.y);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if ([scrollView isEqual:self.myDownScrollView]) {
        self.currentPage = self.myDownScrollView.contentOffset.x / kMyViewFrameW;
        
        [self updateTableWithPageNumber:self.currentPage];
        return;
    }
    [self modifyTopScrollViewPositiong:scrollView];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if ([self.myDownScrollView isEqual:scrollView]) {
        CGRect frame = self.slideView.frame;
        
        frame.origin.x = scrollView.contentOffset.x / kTopBtnCount;
        
        self.slideView.frame = frame;
    }
}


#pragma mark -- TableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    int numberRow = 0;
    if (tableView.tag == 0) {
        numberRow = 6;
    }else if (tableView.tag == 1) {
        numberRow = 1;
    }else if (tableView.tag == 2) {
        numberRow = 16;
    }else {
        numberRow = 4;
    }
    
    return numberRow;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat numberRow = 0.0;
    if (tableView.tag == 0) {
        if (indexPath.row == 0) {
            numberRow = kMyViewFrameW * 0.5;
        }else if (indexPath.row == 1) {
        
            numberRow = kMyViewFrameW * 0.45;
        }else if (indexPath.row == 2) {
            
            numberRow = kMyViewFrameW * 1.5;
        }else if (indexPath.row == 3) {
            
             numberRow = kMyViewFrameW * 1.5;
        }else if (indexPath.row == 4) {
            
             numberRow = kMyViewFrameW * 1.5;
        }else if (indexPath.row == 5) {
            
             numberRow = kMyViewFrameW * 1.5;
        }else {
        
            numberRow = kMyViewFrameW * 1.5;
        }
    }else if (tableView.tag == 1) {
        numberRow = kMyViewFrameH - kUnderViewH;
    }else if (tableView.tag == 2) {
        numberRow = kMyViewFrameW * 0.4;
    }else {
        numberRow = 4;
    }
    return numberRow;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            
             if (tableView.tag == 0) {
                 
//                 if (indexPath.row == 0) {
//                     cell = [[[NSBundle mainBundle] loadNibNamed:@"SlideBarCell" owner:self options:nil]firstObject];
//                     NSLog(@"%f%f",cell.frame.size.width,cell.frame.size.height);
//                 }else if (indexPath.row == 1){
//                 
//                     cell = [[[NSBundle mainBundle] loadNibNamed:@"HYTwoGroupTableViewCell" owner:self options:nil]firstObject];
//                 }else if (indexPath.row == 2){
//                     
//                     cell = [[[NSBundle mainBundle] loadNibNamed:@"HYOneGroupCell" owner:self options:nil]firstObject];
//                 }else if (indexPath.row == 3){
//                     
//                     cell = [[[NSBundle mainBundle] loadNibNamed:@"HYOneGroupCell" owner:self options:nil]firstObject];
//                 }else if (indexPath.row == 4){
//                     cell = [[[NSBundle mainBundle] loadNibNamed:@"HYOneGroupCell" owner:self options:nil]firstObject];
//                     
//                 }else{
//                     
//                     cell = [[[NSBundle mainBundle] loadNibNamed:@"HYOneGroupCell" owner:self options:nil]firstObject];
//                 }
                 
                 int i = (int)indexPath.row;
                 for (i = 0; i < self.oneGroupArray.count; i++) {
                     if (indexPath.row == i) {
                         cell = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@",self.oneGroupArray[i]] owner:self options:nil]lastObject];
                     }else {
                     
                         NSLog(@"buxing");
                     }
                     
                 }
                 
                 
             }else if (tableView.tag == 1) {
                 cell = [[[NSBundle mainBundle] loadNibNamed:@"YEMSongerListView" owner:self options:nil]firstObject];
             }else if (tableView.tag == 2) {
                 cell = [[[NSBundle mainBundle] loadNibNamed:@"HYFourGroupCell" owner:self options:nil]firstObject];
             }else {
             
                 cell = [[[NSBundle mainBundle] loadNibNamed:@"HYTwoGroupTableViewCell" owner:self options:nil]firstObject];
             }
            
        }
    
    
    return cell;
}

@end
